﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Microsoft.Practices.Prism.Commands;
using System.IO;
using SuperKit;

namespace FolderBrowser
{
    public class BrowserViewModel : ViewModelBase
    {
        private string _selectedFolder;

        public string SelectedFolder
        {
            get
            {
                return _selectedFolder;
            }
            set
            {
                _selectedFolder = value;
                OnPropertyChanged("SelectedFolder");
            }
        }

        public ObservableCollection<FolderViewModel> Folders
        {
            get;
            set;
        }

        public DelegateCommand<object> FolderSelectedCommand
        {
            get
            {
                return new DelegateCommand<object>(it => SelectedFolder = Environment.GetFolderPath(Pick(it)));
            }
        }

        private Environment.SpecialFolder Pick(object obj) {
            var s = obj as string;
            if (s == null || s.Length == 0) {
                return Environment.SpecialFolder.LocalApplicationData;
            }
            if (Kit.IsEqual(s, "Desktop")) {
                return Environment.SpecialFolder.Desktop;
            } else if (Kit.IsEqual(s, "MyDocuments")) {
                return Environment.SpecialFolder.MyDocuments;
            } else if (Kit.IsEqual(s, "ProgramFiles")) {
                return Environment.SpecialFolder.ProgramFiles;
            } else if (Kit.IsEqual(s, "Application")) {
                return Environment.SpecialFolder.LocalApplicationData;
            } else if (Kit.IsEqual(s, "MyPictures")) {
                return Environment.SpecialFolder.MyPictures;
            } else {
                return Environment.SpecialFolder.LocalApplicationData;
            }
        }


        public BrowserViewModel()
        {
            Folders = new ObservableCollection<FolderViewModel>();
            Environment.GetLogicalDrives().ToList().ForEach(it => Folders.Add(new FolderViewModel {
                Root = this,
                FolderPath = it.TrimEnd('\\'),
                FolderName = it.TrimEnd('\\'),
                FolderIcon = "/SuperKit;component/Images/HardDisk.ico"
            }));
        }
    }
}
